/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.service.orderManage;

import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.chenweihuacwh.dao.orderManage.DeliveryOrderDAO;
import com.javaeye.chenweihuacwh.dao.orderManage.OrderDAO;
import com.javaeye.chenweihuacwh.dao.orderManage.PurchaseOrderDAO;
import com.javaeye.chenweihuacwh.dao.orderManage.PurchaseOrderDetailDAO;
import com.javaeye.chenweihuacwh.dao.orderManage.ReasonDAO;
import com.javaeye.chenweihuacwh.dto.orderManage.CustomerOrder;
import com.javaeye.chenweihuacwh.dto.orderManage.DeliveryOrder;
import com.javaeye.chenweihuacwh.dto.orderManage.DeliveryOrderDetail;
import com.javaeye.chenweihuacwh.dto.orderManage.PurchaseOrder;
import com.javaeye.chenweihuacwh.dto.orderManage.PurchaseOrderDetail;
import com.javaeye.chenweihuacwh.dto.orderManage.Reason;
import com.javaeye.chenweihuacwh.util.DateUtils;
import com.javaeye.chenweihuacwh.web.PageInfo;
import com.javaeye.chenweihuacwh.web.orderManage.OrderCondition;

public class DeliveryOrderService implements IDeliveryOrderService {
	
	private static Log log = LogFactory.getLog(DeliveryOrderService.class);
	
	private DeliveryOrderDAO dao;
	
	private PurchaseOrderDetailDAO purchaseOrderDetailDao;
	
	private PurchaseOrderDAO purchaseOrderDao;
	
	private OrderDAO orderDao;
	
	private ReasonDAO reasonDao;
	
	public void setDao(DeliveryOrderDAO dao) {
		this.dao = dao;
	}

	public void setPurchaseOrderDetailDao(
			PurchaseOrderDetailDAO purchaseOrderDetailDao) {
		this.purchaseOrderDetailDao = purchaseOrderDetailDao;
	}

	public void setPurchaseOrderDao(PurchaseOrderDAO purchaseOrderDao) {
		this.purchaseOrderDao = purchaseOrderDao;
	}

	public void setOrderDao(OrderDAO orderDao) {
		this.orderDao = orderDao;
	}
	
	public void setReasonDao(ReasonDAO reasonDao) {
		this.reasonDao = reasonDao;
	}

	@SuppressWarnings("unchecked")
	public List getOrderList(OrderCondition condition, PageInfo pageInfo) {
		List<DeliveryOrder> result = dao.getPageData(condition, pageInfo);
		if (result == null || result.size() == 0) {
			log.warn("数据库中未找到指定的成品交运单信息");
		}
		return result;
	}

	public DeliveryOrder getDeliveryOrder(String orderId) {
		DeliveryOrder order = dao.getDeliveryOrder(orderId);
		if (order == null) {
			log.warn("数据库中未找到指定的成品交运单信息，单号：" + orderId);
			return new DeliveryOrder();
		}
		long count = reasonDao.getReasonCount(order.getId());
		order.setReasons(count);
		return order;
	}
	
	public void save(DeliveryOrder order) {
		dao.save(order);
		
		//更新出货单为已经完成
		CustomerOrder customerOrder = orderDao.getOrderBaseInfo(order.getId());
		customerOrder.setStatus(IOrderService.ORDER_STATES_END);
		orderDao.saveOrder(customerOrder);
		
		double hours = DateUtils.getDateOffsetHour(customerOrder.getOutDate(), DateUtils.now());
		if (hours > Reason.LATER_HOUR) {
			Reason reason = new Reason(Reason.REASON_CATEGORY_STORE, 
					order.getId(),
					"仓库发货在出货单的出货日期" + Reason.LATER_HOUR + "个小时之后");
			reasonDao.saveReason(reason);
		}
		
		String purchaseOrderId = order.getPurchaseOrderId();
		Set<DeliveryOrderDetail> details = order.getOrderDetails();
		for (DeliveryOrderDetail detail : details) {
			int productId = detail.getProductId();
			
			//更新订单表里的已经入库数量
			PurchaseOrderDetail purchaseOrderdetail = purchaseOrderDetailDao
					.getPurchaseOrderDetail(purchaseOrderId, productId);
			purchaseOrderdetail.setDeliveryNumber(purchaseOrderdetail.getDeliveryNumber() + detail.getNumber());
			purchaseOrderDetailDao.savePurchaseOrderDetail(purchaseOrderdetail);
		}
		
		//如果全部发货完成更新订单表的状态
		PurchaseOrder purchaseOrder = purchaseOrderDao.getPurchaseOrderInfo(purchaseOrderId);
		Set<PurchaseOrderDetail> alldetails = purchaseOrder.getOrderDetails();
		if (isDeliveryFinished(alldetails)) {
			purchaseOrder.setStatus(IOrderService.ORDER_STATES_END);
			purchaseOrderDao.savePurchaseOrder(purchaseOrder);
		}
	}
	
	/**
	 * 是否全部发货完成
	 * @param details
	 * @return
	 */
	private boolean isDeliveryFinished(Set<PurchaseOrderDetail> details) {
		int allCount = details.size();
		int finishCount = 0;
		for (PurchaseOrderDetail detail : details) {
			if (detail.getDeliveryNumber() >= detail.getNumber()) {
				finishCount++;
			}
		}
		return finishCount == allCount;
	}
}
